import java.util.HashMap;
import java.util.Map;

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class Test3 {
    public Node copyRandomList(Node head){
        if(head == null){
            return null;
        }
        //创建哈希表
        Map<Node,Node> map = new HashMap<>();
        //第一次遍历
        Node curr = head;
        while(curr != null){
            map.put(curr,new Node(curr.val));
            curr = curr.next;
        }
        //第二次遍历
        curr = head;
        while (curr != head){
            Node copyNode = map.get(curr);
            copyNode.next = map.get(curr.next);
            copyNode.random = map.get(curr.random);
            curr = curr.next;
        }
        return map.get(head);
    }
}
